/** @jsxImportSource @emotion/react */
import {
  CCell, Cell, InputLine, LineColumn, SuffixInput, Table, TableBody, TableHead, TableRow, Text
} from "customize-easy-ui-component";
import * as React from "react";
import {InspectRecordLayout, InternalItemProps, RepLink, SelectInput, useItemInputControl} from "../../common/base";
import {calcAverageArrObj, tableSetInp} from "../../../common/tool";
import {useMeasureInpFilter} from "../../common/hooks";
import {检查结论选} from "../../tools";

export const tail转弯半= <Text css={{"@media print": {fontSize: '0.875rem'}}}>
  注：最小转弯半径≤设计值105%。
</Text>;
export const itemA转弯半= ['转弯半表','转弯半v','转弯半r' ] as string[];
const itemAm=itemA转弯半.concat('转弯半径');
export const TurningRadius=
    React.forwardRef((
        { children, show ,alone=true,redId,nestMd,label,}:InternalItemProps,  ref
    ) => {
      const [getInpFilter]=useMeasureInpFilter(null,itemAm,);
      const {inp, setInp} = useItemInputControl({ ref });
      const left=Number(calcAverageArrObj(inp?.转弯半表,(row)=>row?.l, 0,2));
      const right=Number(calcAverageArrObj(inp?.转弯半表,(row)=>row?.r, 0,2));
      const zuo=Number(calcAverageArrObj(inp?.转弯半表,(row)=>row?.z, 0,2));
      const you=Number(calcAverageArrObj(inp?.转弯半表,(row)=>row?.y, 0,2));
      const maxNumber=Math.max(left||0, right||0, zuo||0, you||0);
     //【注意】后右{you||''}；不能改成 后右{you??''}；
      return <InspectRecordLayout inp={inp} setInp={setInp} getInpFilter={getInpFilter} show={show} redId={redId}
                                  nestMd={nestMd} alone={alone} label={label!}>
        <Text variant="h5">{label}：</Text>
        最小转弯半径:设计值= {inp?.转弯半径} mm;  （C3.2.1）测量每次测4个方向；
        <LineColumn column={8}>
          {(new Array(2).fill(null)).map((_,t:number) => {
            return <React.Fragment key={t}>
              <InputLine  label={`${t+1}次前左`}>
                  <SuffixInput  value={inp?.转弯半表?.[t]?.l || ''}
                              onChange={e =>tableSetInp('转弯半表', t, inp, setInp, 'l', e.currentTarget.value || undefined)}>mm</SuffixInput>
              </InputLine>
              <InputLine  label={`${t+1}次前右`}>
                <SuffixInput  value={inp?.转弯半表?.[t]?.r || ''}
                              onChange={e =>tableSetInp('转弯半表', t, inp, setInp, 'r', e.currentTarget.value || undefined)}>mm</SuffixInput>
              </InputLine>
              <InputLine  label={`${t+1}次后左`}>
                <SuffixInput  value={inp?.转弯半表?.[t]?.z || ''}
                              onChange={e => tableSetInp('转弯半表', t, inp, setInp, 'z', e.currentTarget.value || undefined)}>mm</SuffixInput>
              </InputLine>
              <InputLine  label={`${t+1}次后右`}>
                <SuffixInput  value={inp?.转弯半表?.[t]?.y || ''}
                              onChange={e => tableSetInp('转弯半表', t, inp, setInp, 'y', e.currentTarget.value || undefined)}>mm</SuffixInput>
              </InputLine>
            </React.Fragment>;
          })}
        </LineColumn>
        平均值：前左{left||''}；前右{right||''}；后左{zuo||''}；后右{you||''}；
        <LineColumn column={6}>
          <div css={{margin: 'auto'}}>最大值= {maxNumber} mm</div>
          <InputLine  label='测定结果' >
            <SuffixInput  value={inp?.转弯半v ||''}
                          onChange={e=>setInp({ ...inp, 转弯半v: e.currentTarget.value||undefined})}>mm</SuffixInput>
          </InputLine>
          <InputLine label='结果判定'>
            <SelectInput value={inp?.转弯半r || ''} list={检查结论选}
                         onChange={e=>setInp({ ...inp, 转弯半r: e.currentTarget.value||undefined})}/>
          </InputLine>
        </LineColumn>
        {tail转弯半}
      </InspectRecordLayout>;
});
//附表5转弯半径
export const TurningRadiusVw= ({ orc, rep,label,} : { orc: any, rep: any,label:string}
) => {
  const avSpee=calcAverageArrObj(orc?.转弯半表,(row)=>(row?.l ??'')/row?.t, 1,3);
  const left=Number(calcAverageArrObj(orc?.转弯半表,(row)=>row?.l, 0,2));
  const right=Number(calcAverageArrObj(orc?.转弯半表,(row)=>row?.r, 0,2));
  const zuo=Number(calcAverageArrObj(orc?.转弯半表,(row)=>row?.z, 0,2));
  const you=Number(calcAverageArrObj(orc?.转弯半表,(row)=>row?.y, 0,2));
  const maxNumber=Math.max(left||0, right||0, zuo||0, you||0);
  return <>
    <Text id='TurningRadius' variant="h4" css={{marginTop: '1rem',
    }}>{label}</Text>
    <Table fixed={ ["13%","12%","12%","12%","12%","10%","%","9%","9%"] }  css={ {borderCollapse: 'collapse' } } tight  miniw={800}>
      <TableHead>
        <TableRow>
          <CCell>方向</CCell><CCell>前左</CCell><CCell>前右</CCell><CCell>后左</CCell><CCell>后右</CCell>
          <CCell>最大值</CCell><CCell>设计值</CCell><CCell>测定结果</CCell><CCell>结果判定</CCell>
        </TableRow>
      </TableHead>
      <TableBody>
        <RepLink ori rep={rep} tag={'TurningRadius'}>
          {(new Array(2).fill(null)).map((_,t:number) => {
            return <TableRow key={t}>
              {0===t && <CCell rowSpan={2}>转弯半径测量值(mm)</CCell>}
              <CCell>{orc?.转弯半表?.[t]?.l}</CCell><CCell>{orc?.转弯半表?.[t]?.r}</CCell>
              <CCell>{orc?.转弯半表?.[t]?.z}</CCell><CCell>{orc?.转弯半表?.[t]?.y}</CCell>
              {0===t && <><CCell rowSpan={3}>{maxNumber}</CCell><CCell rowSpan={3}>{orc?.转弯半径}</CCell>
                  <CCell rowSpan={3}>{orc?.转弯半v}</CCell><CCell rowSpan={3}>{orc?.转弯半r}</CCell>
              </>}
            </TableRow>;
          })}
          <TableRow>
            <CCell>平均值(mm)</CCell><CCell>{left||''}</CCell><CCell>{right||''}</CCell><CCell>{zuo||''}</CCell><CCell>{you||''}</CCell>
          </TableRow>
          <TableRow>
            <Cell colSpan={9} css={{whiteSpace: 'pre-wrap'}}>{tail转弯半}</Cell>
          </TableRow>
        </RepLink>
      </TableBody>
    </Table>
  </>;
};
